package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.kmproject.haifu.app.hfinterface.AliYunUploadFileInterface;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.HouseReportContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.resbean.AddReportParmer;
import com.kmproject.haifu.mvp.ui.widget.Subject;

import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/10/2019 14:04
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class HouseReportPresenter extends BasePresenter<HouseReportContract.Model, HouseReportContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public HouseReportPresenter(HouseReportContract.Model model, HouseReportContract.View rootView) {
        super(model, rootView);
    }
    /**
     * 上传图片
     * @param
     */
    public void updateHeadImg(int index,Subject subject)
    {
        AppUtil.upfileToAli(mAppManager.getCurrentActivity(), Api.houseSale_server_report, subject.getFilepath(), new AliYunUploadFileInterface() {
            @Override
            public void AliYunUploadFileCallBackProgress(long currentSize, long totalSize) {

            }

            @Override
            public void AliYunUploadFileCallBackSuccess(PutObjectResult result, String serverPath) {
//               mRootView.updHeadPortraitSuccess(serverPath);
                mRootView.updateImgSuccess(index,serverPath);
            }

            @Override
            public void AliYunUploadFileCallBackFailure(ServiceException serviceException) {
                mRootView.updateImgFail(index);
            }
        });


    }
    /**
     * 获取举报原因
     */
    public void  getHousePayWayBean() {

        mModel.getHousePayWayBean(Api.APP_DOMAIN_ZIDIAN+"/app/dictList",Api.REPORT_REASON,"").compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean))
                        {
                            mRootView.getHousePayWayBeanSuccess(listResultBean.getData());
                        }else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }

                    }
                });
    }


    /**
     * 添加举报信inxi
     * @param addReportParmer
     */
    public void  addReport(AddReportParmer addReportParmer)
    {
   mModel.addReport(AppUtil.entityToMap(addReportParmer)).compose(RxUtils.applySchedulers(mRootView))
           .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
               @Override
               public void onNext(ResultBean resultBean) {
                    if (AppUtil.isSuccess(resultBean))
                    {
                        mRootView.addReportSuccess();
                    }else {
                        mRootView.showMessage(resultBean.getMessage());
                    }
               }
           });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
